import { defineStore } from 'pinia'
import { onMounted, ref } from 'vue'
import { createOrderAPI,getCheckInfoAPI,againPayAPI } from '@/apis/checkout'
import { useRouter } from 'vue-router'

export const usePayStore = defineStore('pay',()=>{
  //创建订单
  const payInfo =ref([])
  const order_no = ref()
  const router = useRouter()
  const getOrder = async()=>{
    const a = await getCheckInfoAPI()
    
    order_no.value = a.order_no
    //console.log(order_no.value);
  }
  onMounted(()=>getOrder())
  const createOrder = async()=>{
    const res = await createOrderAPI({
      /* deliveryTimeType: 1,
      payType: 1,
      payChannel: 1,
      buyerMessage:'',
      goods: checkInfo.value.goods.map(item=>{
        return{
          id:item.id,
          count:item.goods_number,
        }
      }),
      addressId:curAddress.value.id, */
      order_no: order_no.value
    })
    
    const orderId = res.fgkey
    payInfo.value = res
    console.log(payInfo.value);
  
  
    router.push({
      path:'/pay',
      query:{
        id : orderId
      }
    })
  }

  //二次支付
  const getId = ref(null)
  const againPayInfo = ref([])
  const again = (data)=>{
    getId.value=data
    console.log(getId.value);
    router.push({
      path:'/again',
      query:{
        id:data
      }
    })
    againPayAPI({
      order_no: data
    })   
    .then(res=>{
      console.log(res);
      againPayInfo.value = res
    }).catch(err=>{
      console.log(err.response.data);
    })
    
  }



  return {
    createOrder,
    payInfo,
    again,
    againPayInfo

  }
}, {
  persist: true,
})